package diagramobjects;
import java.awt.Graphics;
import java.util.ArrayList;

import utils.Color;
import utils.Point;



import program.PaintObjects;

/**
 * 
 * @author wojtek
 * obiekt Linia istniejacy na panelu
 */
public class Line extends DiagramObject {

	protected Integer Id;
	protected Point Begin;
	protected Point End;
	protected Integer Dashing;

	
	
	@Override
	/**
	 * uzywana do rysowania linii na panelu - wywolywana w Drawing Panel
	 */
	public void Draw(Graphics g, PaintObjects po) 
	{
		po.DrawLine(g, this);
	}

	/**
	 * konstruktor
	 * @param beginX 
	 * @param beginY 
	 * @param endX
	 * @param endY
	 */
	public Line(Integer beginX, Integer beginY, Integer endX, Integer endY) {
		super();
		Id = 0;
		Begin = new Point(beginX, beginY);
		End = new Point(endX, endY);
		System.out.println("Creating Line");
		Print();

	}

	public  Line(Integer x, Integer y)
	{
		super();
		Id = 0;
		Begin = new Point(x, y);
		End = new Point(x, y);
		System.out.println("Creating Line");
		Print();
	}

	public Line(Integer id, Point begin, Point end,Integer dashing) 
	{
		super();
		Id = id;
		Begin = begin;
		End = end;	
		Dashing = dashing;

	}


	/**
	 * przesuwa punkt poczatkowy do okreslonej lokalizacji
	 * @param x
	 * @param y
	 */

	public void MoveBeginTo(Integer x, Integer y)
	{
		Begin.setX(x);
		Begin.setY(y);

	}

	
	
	@Override
	public void MoveByVector(Integer vx, Integer vy)
	{
		Begin.MoveByVector(vx, vy);
		End.MoveByVector(vx, vy);
	}

	/**
	 * przesuwa punkt koncowy do okreslonej lokalizacji
	 * @param x
	 * @param y
	 */

	public void MoveEndTo(Integer nowex, Integer nowey)
	{
		End.setX(nowex);
		End.setY(nowey);

	}

	public Integer getBeginX() 
	{
		return Begin.getX();
	}

	public void setBeginX(Integer beginX) 
	{
		Begin.setX(beginX);
	}

	public Integer getBeginY() 
	{
		return Begin.getY();
	}

	public void setBeginY(Integer beginY) 
	{
		Begin.setY(beginY);
	}

	public Integer getEndX() 
	{
		return End.getX();
	}

	public void setEndX(Integer endX) 
	{
		End.setX(endX);
	}

	public Integer getEndY() 
	{
		return End.getY();
	}

	public void setEndY(Integer endY) 
	{
		End.setY(endY);
	}



	/**
	 * sluzy zapisaniu danych do bazy danych - zwraca fragment potrzebnego zapytania
	 * @return
	 */
	public String ToDatabase()
	{

		String toReturn = /* Id.toString() + "," +*/
			Begin.getX().toString() + "," +
			Begin.getY().toString() + "," +
			End.getX().toString() + "," +
			End.getY().toString();

		return toReturn;

	}

	/**
	 * drukuje istniejaca linie - DO TESTOW
	 */

	public void Print()
	{
		String toReturn = Id.toString() + "," +
		Begin.getX().toString() + "," +
		Begin.getY().toString() + "," +
		End.getX().toString() + "," +
		End.getY().toString();

		System.out.println("PRINTING LINE " + toReturn);
	}

	public Boolean PointCollision(Integer x, Integer y)
	{
		Double EndXMBeginX = new Integer(End.getX() - Begin.getX()).doubleValue();
		if (EndXMBeginX  == 0)
			EndXMBeginX = 1.0;
		Double odlOdProstej = -((End.getY()-Begin.getY())/(EndXMBeginX))*x + 1*y - (End.getY() - ((End.getY()-Begin.getY())/(EndXMBeginX))*End.getX());
		System.out.println("ODL" + odlOdProstej);


		if (odlOdProstej < 5)
			return true;
		else
			return false;

	}

	
	@Override
	public Boolean Selection(Integer selLeft, Integer selRight, Integer selTop, Integer selBottom)
	{	
		
		Boolean selected = false;
		System.out.println("LINE SELECTION");
		// equasions for all possibilities
		//1: y = a1*(x-selLeft)   left
		//2: y = a1*(x-selRight) right
		//3: y = selTop top
		//4: y = selBottom bottom
		
		
		
		ArrayList<Double> xs = new ArrayList<Double>();
		ArrayList<Double> ys = new ArrayList<Double>();
		xs.add(selLeft.doubleValue());
		ys.add(selTop.doubleValue());
		
		xs.add(selRight.doubleValue());
		ys.add(selTop.doubleValue());
	
		xs.add(selRight.doubleValue());
		ys.add(selBottom.doubleValue());
		
		xs.add(selLeft.doubleValue());
		ys.add(selBottom.doubleValue());
		
		xs.add(selLeft.doubleValue());
		ys.add(selTop.doubleValue());
		
		Double x1, y1, x2, y2, x3, y3, x4, y4;
		
		x3 = getBeginX().doubleValue();
		y3 = getBeginY().doubleValue();
		x4 = getEndX().doubleValue();
		y4 = getEndY().doubleValue();
		
		if ((selLeft < x3.intValue()) && (x3.intValue() < selRight) && (selLeft < x4.intValue()) && (x4.intValue() < selRight)
			&& (selTop < y3.intValue()) && (y3.intValue() < selBottom) && (selTop < y4.intValue()) && (y4.intValue() < selBottom))
		{
			System.out.println("LINE SELECTION wsystko w zaznaczeniu");
			return true;
		}
		
		
		for (int i = 0; i < 4; ++i)
		{
			x1 = xs.get(i);
			y1 = ys.get(i);
			x2 = xs.get(i+1);
			y2 = ys.get(i+1);

			if ((utils.Usefull.BelongsToSection(x1, y1, x2, y2, x3, y3) == true) ||
				(utils.Usefull.BelongsToSection(x1, y1, x2, y2, x4, y4) == true) ||
				(utils.Usefull.BelongsToSection(x3, y3, x4, y4, x1, y1) == true) ||
				(utils.Usefull.BelongsToSection(x3, y3, x4, y4, x2, y2) == true))
			{
				System.out.println("----------------------BELONGS TRUE");
				return true;
			}
			else
			{
				if ((utils.Usefull.DetM(x1, y1, x2, y2, x3, y3) * utils.Usefull.DetM(x1, y1, x2, y2, x4, y4) <= 0) && 
						(utils.Usefull.DetM(x3, y3, x4, y4, x1, y1) * utils.Usefull.DetM(x3, y3, x4, y4, x2, y2) <= 0))
				{
					System.out.println("----------------------SELECTED DETS1!!!");
					return true;
				
				}
				
				
			}
		}
		
		return false;
		
	}
	
	@Override
	public void DrawSelection(Graphics g, PaintObjects po)
	{
		// TODO Auto-generated method stub
		po.DrawLineSelection(g, this);
	}

}

